package cn.stylefeng.guns.modular.auth.domain.visitor;

import cn.stylefeng.guns.modular.auth.domain.composite.PriorityNode;
import cn.stylefeng.guns.modular.auth.domain.dto.AuthPriorityDTO;
import cn.stylefeng.guns.modular.auth.entity.AuthAccountPriorityRelationshipEntity;
import cn.stylefeng.guns.modular.auth.entity.AuthRolePriorityRelationshipEntity;
import cn.stylefeng.guns.modular.auth.mapper.AuthPriorityMapper;
import cn.stylefeng.guns.modular.auth.mapper.AuthRolePriorityRelationshipMapper;
import cn.stylefeng.guns.modular.auth.service.AuthAccountPriorityRelationshipService;
import cn.stylefeng.guns.modular.auth.service.AuthPriorityService;
import cn.stylefeng.guns.modular.auth.service.AuthRolePriorityRelationshipService;
import cn.stylefeng.guns.modular.auth.utils.converter.AuthPriorityDTO2NodeConverter;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.Mapper;

import java.util.List;
import java.util.Optional;

/**
 * 检查权限树有没有被关联
 *
 * @author allen
 * @version 1.0
 * @date 2021/4/12 23:56
 */
@Slf4j
public class PriorityNodeRelateCheckVisitor implements PriorityNodeVisitor {

//    public Boolean getRelateCheckResult() {
//        return relateCheckResult;
//    }

    @Getter
    private Boolean relateCheckResult=false;

    private AuthPriorityService dao;

    private AuthAccountPriorityRelationshipService authAccountPriorityRelationshipService;

    private AuthRolePriorityRelationshipService authRolePriorityRelationshipService;


    public PriorityNodeRelateCheckVisitor(AuthPriorityService dao,
                                          AuthAccountPriorityRelationshipService authAccountPriorityRelationshipService,
                                          AuthRolePriorityRelationshipService authRolePriorityRelationshipService) {
        this.dao = dao;
        this.authAccountPriorityRelationshipService = authAccountPriorityRelationshipService;
        this.authRolePriorityRelationshipService = authRolePriorityRelationshipService;
//        this.relateCheckResult = false;
    }

    /**
     * 检查node有没有被关联
     *
     * @param node 节点
     */
    @Override
    public void visit(PriorityNode node) {

        List<AuthPriorityDTO> children = dao.getPrioritiesByParentId(node.getId());

        List<PriorityNode> priorityNodes = AuthPriorityDTO2NodeConverter.INSTANCE.sourceToTarget(children);
        for (PriorityNode priorityNode : priorityNodes) {
            priorityNode.accept(this);
        }
        if (relateCheck(node)) {
            this.relateCheckResult = true;
        }


    }


    private Boolean relateCheck(PriorityNode node) {
        Boolean existAccountRecord = authAccountPriorityRelationshipService.existRecordByPriorityId(node.getId());
        if (existAccountRecord) {
            log.info("==========发现跟账号有关联");
            return true;
        }

        Boolean existRoleRecord = authRolePriorityRelationshipService.existRecordByPriorityId(node.getId());
        if (existRoleRecord) {
            log.info("==========发现跟角色有关联");

            return true;
        }

        return false;
    }
}
